//get the password input, go up to parent's parent and insert new row
function addServerBox() {
    if (document.getElementById('server') != undefined)
        return;
    
    var pword = document.getElementById('pword');
    var tr = pword.parentNode;
    tr = tr.parentNode;
    var serverTr = document.createElement('tr');
    var td1 = document.createElement('td');
    var td2 = document.createElement('td');
    var serverBox = document.createElement('input');
    serverBox.setAttribute('type', 'textbox');
    serverBox.setAttribute('id', 'server');
    
    td2.appendChild(serverBox);
    td1.appendChild(document.createTextNode('Server'));
    serverTr.appendChild(td1);
    serverTr.appendChild(td2);

    var table = document.getElementsByTagName('tbody')[0];
    table.insertBefore(serverTr, tr.nextSibling);
}

//remove the row containing the server input if the selected radio button isn't
//the JABBER one.
function removeServerBox() {
    if (document.getElementById('server') == undefined)
        return;
    var server = document.getElementById('server');
    var tr = server.parentNode;
    tr = tr.parentNode;
    document.getElementsByTagName('tbody')[0].removeChild(tr);
}

function getElementsByClassName(name) {
    var body = document.getElementsByTagName('body')[0];
    var collection = new Array();
    
    var treeWalker = document.createTreeWalker(body, NodeFilter.SHOW_ELEMENT, null, false);    
    while (treeWalker.nextNode()) {
        if (treeWalker.currentNode.className == name) {
            collection.push(treeWalker.currentNode);
        }
    }    
    return collection;
}

//defines the slider class; the backend for the slider.
function Slider(sliderElement, left, right) {
    this.isMoving;
    this.lastXPos;
    this.leftElement;
    this.rightElement;
    this.sliderNode;
    
    if (called_proto == undefined) {
        var called_proto = true;
        this.sliderNode = sliderElement;
        this.lastXPos = this.sliderNode.offsetLeft + this.sliderNode.clientWidth / 2;
        this.isMoving = false;
        this.rightElement = right;
        this.leftElement = left;
        Slider.prototype.moveSlider = moveSlider;
        Slider.prototype.beginMove = beginMove;
        Slider.prototype.stopMove = stopMove;
    }
    
    //resizes the left and right divs and repositions (if neccessary) the slider div.
    function moveSlider(currentXPos) {
        if (this.isMoving) {
            var delta = currentXPos - this.lastXPos;           
            this.leftElement.width = this.leftElement.offsetLeft - delta;
            this.rightElement.width = this.rightElement.offsetLeft + delta;
            this.lastXPos = this.sliderNode.offsetLeft + this.sliderNode.clientWidth / 2;
        }
    }
    
    function beginMove() {
        this.isMoving = true;
    }
    
    function stopMove() {
        this.isMoving = false;
    }
}

var sliderObj = new Slider(document.getElementById('slider'));

function onSepMouseDown(event) {
    sliderObj.beginMove();
}

function onSepMouseUp(evt) {
    sliderObj.stopMove();
}

function onSepMouseMove(evt) {
    sliderObj.moveSlider(evt.pageX);
}

//timer object that allows timer instances to persist across function calls.
function Timer() {
    this._timerInstance;
    
    if(proto_called == undefined) {
        var proto_called = true;
        Timer.prototype.startTimer = startTimer;
        Timer.prototype.stopTimer = stopTimer;
        Timer.prototype.startRepeatingTimer = startRepeatingTimer;
        Timer.prototype.stopRepeatingTimer = stopRepeatingTimer;
    }
    
    function startTimer(callback, interval) {
        this._timerInstance = setTimeout(callback, interval);
    }
    
    function stopTimer() {
        clearTimeout(this._timerInstance);
    }
    
    function startRepeatingTimer(callback, interval) {
        this._timerInstance = setInterval(callback, interval);
    }
    
    function stopRepeatingTimer() {
        clearInterval(this._timerInstance);
    }
}

